import { Component, OnInit , ViewChild ,TemplateRef, ElementRef } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { NzLayoutModule } from 'ng-zorro-antd/layout';
import { NzButtonModule, NzButtonComponent  } from 'ng-zorro-antd/button';
import { NzFormatEmitEvent  } from 'ng-zorro-antd/core';
import { NzMessageService, NzModalRef, NzModalService, UploadXHRArgs, UploadChangeParam, UploadFile } from 'ng-zorro-antd';
import { FormBuilder, FormControl, FormGroup  } from '@angular/forms';
import { DelonFormModule,SFSchema , SFComponent , WidgetRegistry, SFSchemaEnumType, SFTextareaWidgetSchema } from '@delon/form';
import { HttpService, SupDic, GridComponent } from 'lbf';
import { Subject } from 'rxjs';
import { STColumn, STColumnTag, STColumnBadge } from '@delon/abc';

@Component({
  selector: 'app-dzda-zcyjwh',
  templateUrl: './zcyjwh.component.html',
  styleUrls: ["./zcyjwh.component.less"]
})
export class DzdaZcyjwhComponent implements OnInit {
  zcyjSearchSchema: SFSchema={properties:{}};
  queryFormData={};
  formData={};
  @ViewChild('zcyjSearchSF',{static:false}) zcyjSearchSF: SFComponent;  

  @ViewChild('zcyjST',{static:false}) zcyjST: GridComponent;  
  zcyjColumns: STColumn[] = [
    {title:"ID",index:"DAD181",width:'10%'},
    // {title:"状态",index:"AAE100",width:'100px',render:"custom"},
    {title:"依据类别",index:"DAD170",width:'20%',dic:'DAD170'},
    {title:"依据名称",index:"DAD182",width:'60%',className:"cjwt_st_hidden_120"},
    // {title:"法律依据",index:"DAD171",width:'520px',className:"cjwt_st_hidden"},
    // {title:"备注",index:"DAD183",width:'220px',className:"cjwt_st_hidden_220"},
    { title: '操作区', fixed:'right',width:'10%',
      buttons:[
      { 
        text: '修改',
        icon: 'edit',
        click: (record: any) => this.zcyjUpdate(record),
      },
      { 
        text: '删除',
        icon: 'delete',
        click: (record: any) => this.zcyjDelete(record),
        pop: true,
        popTitle: '确认删除该依据吗？',
      },
      { 
        text: '更多',
        // click: (record: any) => this.zcyjUpdate(record),
        children: [
          { 
            text: '附件维护',
            icon: 'edit',
            click: (record: any) => this.fjUpdate(record),
          },
          { 
            text: '标签维护',
            icon: 'edit',
            click: (record) => this.addClbq(record),
          },
        ]
      },
    ]},
  ];
  // 添加
  @ViewChild('zcyjAddSF',{static:false}) zcyjAddSF: SFComponent;  
  zcyjAddSchema: SFSchema={properties:{}};
  zcyjAddVisible=false;
  // 修改
  @ViewChild('zcyjUpdateSF',{static:false}) zcyjUpdateSF: SFComponent;  
  zcyjUpdateSchema: SFSchema={properties:{}};
  zcyjUpdateVisible=false;

  /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
  constructor(private lbservice: HttpService,
    private supdic: SupDic,
    private msgSrv: NzMessageService) { }

  ngOnInit() { 
    this.initZcyjSearchSchema();
  }


  checkValueNull(e:string){
    if(e==null||e.length===0||e===undefined){
      return true;
    }
    return false;
  }

  checkValueNumber(sch:string,tar:string,src:string,title:string){
    if(this.checkValueNull(this[sch].value[src])){
      return true;
    }
    this[sch].value[tar]=Number(this[sch].value[src]);
    if(title!=='none' && isNaN(this[sch].value[tar])){
      this.msgSrv.error(title+'只允许为数字');
      return false;
    }
    return true;
  }

  changeState(index: any) {
    const rec: any = this.zcyjST.data[index];
      if(rec.AAE100 === '1') {
        rec.AAE100 = '0';
      }else{
        rec.AAE100 = '1';
      }
    this.lbservice.lbservice('ZCYJMLWH_CSZCYJML', { para: rec }).then(resdata => {
      if (resdata.code === 1) {
        this.zcyjST.data[index].AAE100 = rec.AAE100;
      }else{
        if (rec.AAE100 === '1') {
          rec.AAE100 = '0';
        }else{
          rec.AAE100 = '1';
        }
        this.zcyjST.data[index].AAE100 = rec.AAE100;
      }
    });
  }
  /****************************************查询****************************************** */
  zcyjQuery(cpi=false,pin=1){
    if(cpi){
      this.zcyjST.pi=pin;
    }
    this.zcyjST.reload(this.zcyjSearchSF.value);
  }

  initZcyjSearchSchema(){
    this.zcyjSearchSchema= {
      properties: {
        DAD170: {
          type: 'string',
          title: '依据级别',
          enum:this.supdic.getSFDic('DAD170'),
          ui:{
            widget: 'select',
            allowClear:true,
          }
        },
        DAD182: {
          type: 'string',
          title: '依据名称',
        },
        DAD130: {
          type: 'string',
          title: '材料标签',
          enum: this.supdic.getSFDic('ZCYJBQ'),
          ui: {
            widget: 'select',
            mode: 'tags',
            width: 400,
            allowClear: true
          }
        }
      },
      ui:{
        spanLabelFixed:100,
        grid:{span:8},
      },
    };
  }

  /****************************************添加****************************************** */
  initzcyjAddSchema(){
    this.zcyjAddSchema= {
      properties: {
        DAD170: {
          type: 'string',
          title: '依据级别',
          enum:this.supdic.getSFDic('DAD170'),
          default:'1',
          ui:{
            widget: 'select',
            grid:{span:12},
          }
        },
        DAD182: {
          type: 'string',
          title: '依据名称',
          ui:{
            grid:{span:24},
          }
        },
        // DAD171:{
        //   type:'string',
        //   title:'法律依据',
        //   ui:{
        //     widget: 'textarea',
        //     autosize: { minRows: 3, maxRows: 8 },
        //     grid:{span:24},
        //   }
        // },
        // DAD183:{
        //   type:'string',
        //   title:'备注',
        //   ui:{
        //     widget: 'textarea',
        //     autosize: { minRows: 2, maxRows: 86},
        //     grid:{span:24},
        //   }
        // },
      },
      ui:{
        spanLabelFixed:100,
        grid:{span:24},
      },
      required:['DAD170',"DAD182"],
    };
  }
  gotoZcyjAdd(){
    this.initzcyjAddSchema();
    this.zcyjAddVisible=true;
  }
  zcyjAddCancel(){
    this.zcyjAddVisible=false;
  }
  zcyjAddOk(){
    if(this.checkValueNull(this.zcyjAddSF.value.DAD182)){
      this.msgSrv.error('依据名称不可为空');
      return;
    }
    // if(this.checkValueNull(this.zcyjAddSF.value.DAD171)){
    //   this.msgSrv.error('法律依据不可为空');
    //   return;
    // }
    this.lbservice.lbservice("ZCYJMLWH_AZCYJML",{para:this.zcyjAddSF.value}).then(resdata=>{
      if(resdata.code>0){
        this.msgSrv.success('添加成功');
        this.zcyjQuery(true);
        this.zcyjAddVisible=false;
      }else{
        this.msgSrv.error(resdata.errmsg);
      }
    })
  }

    /****************************************修改****************************************** */
  initzcyjUpdateSchema(rec:any){
    this.zcyjUpdateSchema= {
      properties: {
        DAD181:{
          type:'string',
          title:'',
          default:rec.DAD181,
          ui:{
            hidden:true,
          }
        },
        DAD170: {
          type: 'string',
          title: '依据级别',
          enum:this.supdic.getSFDic('DAD170'),
          default:rec.DAD170,
          ui:{
            widget: 'select',
            grid:{span:12},
          }
        },
        DAD182: {
          type: 'string',
          title: '依据名称',
          default:rec.DAD182,
          ui:{
            grid:{span:24},
          }
        },
        // DAD171:{
        //   type:'string',
        //   title:'法律依据',
        //   default:rec.DAD171,
        //   ui:{
        //     widget: 'textarea',
        //     autosize: { minRows: 3, maxRows: 8 },
        //     grid:{span:24},
        //   }
        // },
        // DAD183:{
        //   type:'string',
        //   title:'备注',
        //   default:rec.DAD183,
        //   ui:{
        //     widget: 'textarea',
        //     autosize: { minRows: 2, maxRows: 86},
        //     grid:{span:24},
        //   }
        // },
      },
      ui:{
        spanLabelFixed:100,
        grid:{span:24},
      },
      required:['DAD170',"DAD182"],
    };
  }

  zcyjUpdate(rec:any){
    this.initzcyjUpdateSchema(rec);
    this.zcyjUpdateVisible=true;
  }

  zcyjUpdateCancel(){
    this.zcyjUpdateVisible=false;
  }
  zcyjUpdateOk(){
    if(this.checkValueNull(this.zcyjUpdateSF.value.DAD182)){
      this.msgSrv.error('依据名称不可为空');
      return;
    }
    // if(this.checkValueNull(this.zcyjUpdateSF.value.DAD171)){
    //   this.msgSrv.error('法律依据不可为空');
    //   return;
    // }
    this.lbservice.lbservice("ZCYJMLWH_UZCYJML",{para:this.zcyjUpdateSF.value}).then(resdata=>{
      if(resdata.code>0){
        this.msgSrv.success('修改成功');
        this.zcyjQuery();
        this.zcyjUpdateVisible=false;
      }else{
        this.msgSrv.error(resdata.errmsg);
      }
    })
  }
  /****************************************删除****************************************** */
  zcyjDelete(rec:any){
    this.lbservice.lbservice("ZCYJMLWH_DZCYJML",{para:{DAD181:rec.DAD181}}).then(resdata=>{
      if(resdata.code>0){
        this.msgSrv.success('删除成功');
        if(this.zcyjST.st.total%10===1&&Math.ceil(this.zcyjST.st.total/10)===this.zcyjST.pi){
          this.zcyjQuery(true,this.zcyjST.pi-1);
        }else{
          this.zcyjQuery();
        }
        // this.zcyjAddVisible=false;
      }else{
        this.msgSrv.error(resdata.errmsg);
      }
    })
  }

  /**************************************标签维护************************************** */
  DAD181;
  modalBqTitle="";
  tags=[];
  isBqVisible = false;
  inputVisible=false;
  @ViewChild('inputElement', { static: false }) inputElement: ElementRef;
  inputValue = '';

  addClbq(rec){
    this.DAD181 = rec.DAD181;
    this.modalBqTitle = this.sliceString(rec.DAD182)+ ' 新增标签';
    this.lbservice.lbservice('ZCYJMLWH_QZCYJBQ',{DAD181: rec.DAD181}).then(res => {
      if(res.code >0 ){
        this.tags = res.message.list;
        this.isBqVisible = true;
      }else{
        this.msgSrv.error(res.errmsg);
      }
    })
  }

  sliceString(str){
    return str.length>16 ? `${str.slice(0, 20)}...` : str;
  }

  BQAddhandleCancel(){
    this.isBqVisible=false;
  }

  handleClose(removedTag: {}): void {
    this.tags = this.tags.filter(tag => tag !== removedTag);
  }

  sliceTagName(tag: string): string {
    const isLongTag = tag.length > 20;
    return isLongTag ? `${tag.slice(0, 20)}...` : tag;
  }

  showInput(): void {
    this.inputVisible = true;
    setTimeout(() => {
      this.inputElement.nativeElement.focus();
    }, 10);
  }

  handleInputConfirm(): void {
    if (this.inputValue && this.tags.indexOf(this.inputValue) === -1) {
      this.tags = [...this.tags, this.inputValue];
    }
    this.inputValue = '';
    this.inputVisible = false;
  }

  savecl(){
    this.lbservice.lbservice('ZCYJMLWH_AZCYJBQ',{DAD181: this.DAD181,TAGS: this.tags}).then(res => {
      if(res.code >0){
        this.msgSrv.success("材料标签更改成功")

        this.zcyjSearchSchema.properties.DAD130.enum = res.message.dic;
        this.zcyjSearchSF.refreshSchema();
      }
    })
    this.isBqVisible = false;
  }
/**********************************附件维护********************************************* */
fjVisible=false;
fjModalTitle="";
fileList=[];

fjUpdate(rec){
  this.paras={DAD181:rec.DAD181};
  this.fjVisible=true;
}
fjAddCancel(){
  this.fjVisible=false;
}
paras = {};
Sname :FileSname = {save:'ZCYJMLWH_UploadFile',query:'ZCYJMLWH_QueryFile',del:'ZCYJMLWH_DeleteFile'}
addDone(event) {
  console.log(event);
}






}

interface FileSname {
  /**
   * 文件上传
   */
  save?: string;
  /**
   * 文件预览
   */
  query: string;
  /**
   * 文件删除
   */
  del?: string;
}
